﻿
package system.base;

import java.sql.ResultSet;

import framework.core.*;
import system.SelectManager;
import system.data.*;

/*
    Author    :justin
    CreateTime:2007-12-13 17:36:20
*/
public class Warehouse extends WarehouseBase 
{

    public final static String WAREHOUSE_CODE_HEAD = "0000";  //总库
    public final static String WAREHOUSE_CODE_HEAD_NAME = "总部仓库";   //总部仓库
    
//----system generated start----//  
public Warehouse() 
{
    
}

private static void delete(DBConnection con,String code) throws Exception
{
    Conditions conditions = new Conditions();

    conditions.add(CODE,code);
    
    delete(con,TABLE_NAME,conditions);
}
public static Warehouse fetch()
{
    return new Warehouse();
}
public static Warehouse copy(Warehouse srcObj)
{
    Warehouse obj = fetch();
    obj.copyBase(srcObj);
    return obj;
}
public static Warehouse fetch(DBConnection con,String id) throws Exception
{
    Conditions conditions = new Conditions();

    conditions.add(ID,id);
    
    return fetch(con,conditions);
}
public static Warehouse fetch(DBConnection con,Conditions conditions) throws Exception
{
    DBResult result = null;
    try
    {
        result = DBEngine.doSelect(con,"select * from " + TABLE_NAME,conditions);
        java.sql.ResultSet rs = result.getRs();
        Warehouse obj = null;
        if(rs.next())
        {
            obj = fetch(rs);
        }
        return obj;
    }
    finally
    {
        if(result!=null) result.clear() ;
    }
}
public static Warehouse fetch(java.sql.ResultSet rs) throws Exception
{
    Warehouse obj = new Warehouse();
    obj.initObject(rs);
    return obj;
}
protected void getUnique(DBConnection con) throws Exception
{
        setId(DBEngine.getId(con));
}
//----system generated end----//



//----add by justin start----//
 
  public String viewStatus(DBConnection con) throws Exception
  {
    return SelectManager.viewYesOrNo(getStatus()+"");   
  } 
  
  private void checkUnique(DBConnection con) throws Exception 
  {
      Conditions cs = new Conditions();
      cs.add(CODE, getCode());
      cs.add(ID, Conditions.NOT_EQUAL, getId());
      if(DBEngine.doCount(con, "select count(id) from "+TABLE_NAME, cs) > 0){
          setId("");
          throw new BizException("已存在的编码，请重新填写。");
      }
  }
  public void beforeUpdate(DBConnection con) throws Exception
  {
      checkUnique(con);
      setLastUpdateTime(new DateUtil().getFullString());      
  }
  public void beforeInsert(DBConnection con) throws Exception
  {
      checkUnique(con);
      DateUtil du = new DateUtil();
      setCreateTime(du.getFullString());
      setLastUpdateTime(du.getFullString());
  }  
 
  //获取当前仓库及下级仓库
  public static ObjectList fetchAllOrgs(DBConnection con,String currWareCode) throws Exception
  {
        String sqlWareCode = ConstDefine.getWildCard(currWareCode);
        
        Values vs = new Values();
        DBEngine.checkCon(con);
        String sql = "select * from " + Warehouse.TABLE_NAME + " where " + Warehouse.CODE + " like ? and is_show='1' order by " + Warehouse.CODE;
        vs.add(sqlWareCode+"%");
        
        DBResult result = DBEngine.doSelect(con,sql,vs);
        
        ObjectList list = ObjectList.fromRs(Warehouse.class,result.getRs());
        result.clear();
        return list;    
  } 
  
  //获取当前仓库及下级仓库
  public static ObjectList fetchAllOrgsByOwnWarehouseCode(DBConnection con,String currWareCode) throws Exception
  {

        Values vs = new Values();
        DBEngine.checkCon(con);
        String sql = "select * from " + Warehouse.TABLE_NAME + " where " + Warehouse.CODE + " like ? and is_show='1' order by " + Warehouse.CODE;
        vs.add(currWareCode+"%");
        
        DBResult result = DBEngine.doSelect(con,sql,vs);
        
        ObjectList list = ObjectList.fromRs(Warehouse.class,result.getRs());
        result.clear();
        return list;    
  } 
  

  /** 获得当前仓库及其下属仓库    (用于修改后的仓库关系逻辑) add by zhengbin*/
  public static ObjectList fetchWarehouses(DBConnection con,String currWareCode) throws Exception{
        DBEngine.checkCon(con);
        String sql = "select * from t_warehouse where code like '"+currWareCode+"%' and is_show='1'  order by code,length(code)";
        DBResult result = DBEngine.doSelect(con,sql);
        ObjectList list = ObjectList.fromRs(Warehouse.class,result.getRs());
        result.clear();
        return list;    
  }
  
  /** 获得当前仓库及其下属仓库    (用于修改后的仓库关系逻辑) add by zhengbin*/
  public static ObjectList fetchWarehousesList(DBConnection con,String currWareCode) throws Exception{
        DBEngine.checkCon(con);
        String sql = "select * from t_warehouse where code like '"+currWareCode+"%' and is_show='1' and code not like '000005%' and code not in ('00000202','00000300','00000301','00000302')  order by code,length(code)";
        DBResult result = DBEngine.doSelect(con,sql);
        ObjectList list = ObjectList.fromRs(Warehouse.class,result.getRs());
        result.clear();
        return list;    
  }
  
  //获取当前仓库及下级仓库
  public static ObjectList fetchAllOrgsByShow(DBConnection con,String currWareCode) throws Exception
  {
        String sqlWareCode = ConstDefine.getWildCard(currWareCode);
        
        Values vs = new Values();
        DBEngine.checkCon(con);
        String sql = "select * from " + Warehouse.TABLE_NAME + " where " + Warehouse.CODE + " like ? and is_show = '1' order by " + Warehouse.CODE;
        vs.add(sqlWareCode+"%");
        
        DBResult result = DBEngine.doSelect(con,sql,vs);
        
        ObjectList list = ObjectList.fromRs(Warehouse.class,result.getRs());
        result.clear();
        return list;    
  } 
  
  /** 获得当前仓库及其下属仓库 字符串*/
  public static String fetchWarehousesForString(DBConnection con,String currWareCode) throws Exception{
        DBEngine.checkCon(con);
        String warehouse = "";
        String sql = "select code from t_warehouse where code like '"+currWareCode+"%' and status='1' order by code,length(code)";
        DBResult result = DBEngine.doSelect(con,sql);
        ResultSet set= result.getRs();
        while(set.next()){
            warehouse += set.getString("code")+",";
        }
        result.clear();
        return warehouse.substring(0,warehouse.length()-1);    
  } 
  
  //通过仓库编码获取仓库实例
  public static Warehouse fetchByCode(DBConnection con,String code) throws Exception
  {
      Conditions conditions = new Conditions();

      conditions.add(CODE,code);
    
      return fetch(con,conditions);
  } 
  
  //通过编码得到名称
  public static String getNameByCode(DBConnection con,String code) throws Exception
  {
      String mReturn = "";
      Warehouse w = fetchByCode(con,code);
      if (w != null)
      {
          mReturn = w.getName();
      }
      
      return mReturn;
  }  


//----add by justin end----//

//----add by Arrow---//

//计算分层编码
 public static String getLevelCode(String code) throws Exception{
     if(code == null) code = "";
     if("".equals(code)) return code; 
     String code1 = code.substring(0, 2);
     String code2 = code.substring(2, 4);
     String code3 = code.substring(4, 6);
     String ret = "";
     
     if("00".equals(code3)){
         if("00".equals(code2)){
             ret = code1;
         }
         else{
             ret = code1 + code2;
         }
     }
     else{
         ret = code1 + code2 + code3;
     }
     
     return ret;
 }
 
//计算编码层次
 public static int getLevel(String code) throws Exception{
     if(code == null) code = "";
     if("".equals(code)) return 0; 
     String code1 = code.substring(0, 2);
     String code2 = code.substring(2, 4);
     String code3 = code.substring(4, 6);
     int ret = 0;
     
     if("00".equals(code3)){
         if("00".equals(code2)){
             ret = 1;
         }
         else{
             ret = 2;
         }
     }
     else{
         ret = 3;
     }
     
     return ret;
 }
//----add by Arrow end---//
 
 public static String getWarehouseTree(ObjectList warehouseList,String[] warehouseCodes) throws Exception{

    Warehouse warehouse = (Warehouse)warehouseList.get(0);
    if(warehouse.getCode().length() <= 4){
            return getHtmlDateByCodeless(warehouseList, warehouse,warehouseCodes);
    }else if(warehouse.getCode().length() == 6){
        return getHtmlDateByCodelessSix(warehouseList, warehouse,warehouseCodes);
    }else if(warehouse.getCode().length() == 8){
        return getHtmlDateByCodelessSix(warehouseList, warehouse,warehouseCodes);
    }else if(warehouse.getCode().length() == 10){
        return getHtmlDateByCodelessTen(warehouseList, warehouse,warehouseCodes);//一电仓库特例
    }else{
        return "与服务器连接发送错误！";
    }
 }

    private static String getHtmlDateByCodeless(ObjectList warehouseList,
            Warehouse warehouse,String[] warehouseCodesArray) throws Exception {
        String htmlStart = "";
        String htmlEnd = "";
        String htmlBody = "";
        htmlStart += "  <ul>"
                  +  "      <li class=\"jstree-open \" id = \""+warehouse.getCode()+"\" >"
                  +  "      <a href=\"#\">"+warehouse.getName()+"</a>"
                  +  "          <ul>";

          htmlEnd += "          </ul>"
                  +  "      </li>"
                  +  "  </ul>";

          String warehouseChildBody = "";
          int number = 0;
          int serviceNumber = 0;
          boolean isChild=false;
          for (int i = 1; i < warehouseList.size(); i++) {
             Warehouse warehouseChild = (Warehouse) warehouseList.get(i);
             if(warehouseChild.getCode().length()==4){
                 if(number>0){
                     htmlBody += "</ul></li></ul></li>";
                     htmlBody += "<li class=\"jstree-open \"id = \""+((Warehouse)warehouseList.get(i)).getCode()+"\" >"
                        +  "                    <a href=\"#\">"+((Warehouse)warehouseList.get(i)).getName()+"</a>"
                        +  "                    <ul>";
                     serviceNumber=0;
                 }else{
                     htmlBody  += "<li class=\"jstree-open \" id = \""+((Warehouse)warehouseList.get(i)).getCode()+"\" >"
                            +  "                    <a href=\"#\">"+((Warehouse)warehouseList.get(i)).getName()+"</a>"
                            +  "                    <ul>";
                     number++;  
                 }
             }else if(warehouseChild.getCode().length()==6){
                if(isChild){
                    warehouseChildBody +="</ul></li>";
                    htmlBody +=warehouseChildBody.substring(10,warehouseChildBody.length());
                    isChild=false;
                }
                if(serviceNumber>0){
                    if(number>1){
                        htmlBody +=" <li id = \""+warehouseChild.getCode()+"\">"  
                         + "    <a href=\"#\">"+warehouseChild.getName()+"</a>"
                         + "    <ul>";
                    }else{
                        htmlBody +="</ul></li>";
                        htmlBody +=" <li id = \""+warehouseChild.getCode()+"\">"  
                         + "    <a href=\"#\">"+warehouseChild.getName()+"</a>"
                         + "    <ul>";
                    }
                    
                }else{
                    htmlBody +=" <li id = \""+warehouseChild.getCode()+"\">"  
                     + "    <a href=\"#\">"+warehouseChild.getName()+"</a>"
                     + "    <ul>";
                    serviceNumber++;
                }
             }else if(warehouseChild.getCode().length()==8 ){
                 //判断是否为电车，先写死
                if(warehouseChild.getCode().substring(0, 6).equals("000004")){
                    warehouseChildBody += "</ul></li>" 
                             +  "<li id=\""+warehouseChild.getCode()+"\"> "
                             +  "    <a href=\"#\">"+warehouseChild.getName()+"</a>"
                             +  "    <ul>";
                    isChild = true;
                }else{
                    htmlBody +="<li id=\""+warehouseChild.getCode()+"\" ><a href=\"#\">"+warehouseChild.getName()+"</a></li>";  
                }
             }else if(warehouseChild.getCode().length() ==10){
                warehouseChildBody += "<li id=\""+warehouseChild.getCode()+"\"><a href=\"#\">"+warehouseChild.getName()+"</a></li>";
             }
          }
         htmlBody+="</ul></li></ul></li>";
         return  htmlStart + htmlBody + htmlEnd;
    }
    
    private static String getHtmlDateByCodelessSix(ObjectList warehouseList,
            Warehouse warehouse,String[] warehouseCodesArray) throws Exception {
         String htmlBody = "";
         String warehouseChildBody = "";
         String htmlStart = "";
         String htmlEnd = "";
         htmlStart +="  <ul>    ";
         htmlEnd   +="  </ul>   ";
         boolean isChild=false;
         boolean isKill = false;
         for (int i = 0; i < warehouseList.size(); i++) {
             Warehouse warehouseChild = (Warehouse) warehouseList.get(i);
             if(warehouseChild.getCode().length()==6){
                if(isChild){
                    warehouseChildBody +="</ul></li>";
                    htmlBody +=warehouseChildBody.substring(10,warehouseChildBody.length());
                    isChild=false;
                }
                htmlBody +="</ul></li>";//最后截掉
                htmlBody +=" <li class=\"jstree-open \" id = \""+warehouseChild.getCode()+"\">"  
                         + "    <a href=\"#\">"+warehouseChild.getName()+"</a>"
                         + "    <ul>";
                isKill = true;
             }else if(warehouseChild.getCode().length()==8 ){
                 //判断是否为电车，先写死
                if(warehouseChild.getCode().substring(0, 6).equals("000004")){
    
                    warehouseChildBody += "</ul></li>" 
                             +  "<li id=\""+warehouseChild.getCode()+"\"> "
                             +  "    <a href=\"#\">"+warehouseChild.getName()+"</a>"
                             +  "    <ul>";
                    isChild = true;
                }else{
                    htmlBody +="<li id=\""+warehouseChild.getCode()+"\" ><a href=\"#\">"+warehouseChild.getName()+"</a></li>";                      
                }
             }else if(warehouseChild.getCode().length() ==10){
                warehouseChildBody += "<li id=\""+warehouseChild.getCode()+"\"><a href=\"#\">"+warehouseChild.getName()+"</a></li>";
             }
          }
         if(((Warehouse) warehouseList.get(0)).getCode().substring(0, 6).equals("000004")){
             if(isChild){
                    warehouseChildBody +="</ul></li>";
                    htmlBody +=warehouseChildBody.substring(10,warehouseChildBody.length());
                    isChild=false;
                }
         }
         htmlBody+="</ul></li>";
         if(isKill){
             htmlBody = htmlBody.substring(10, htmlBody.length());
         }
         return  htmlStart+htmlBody+htmlEnd;
    }
    
    private static String getHtmlDateByCodelessTen(ObjectList warehouseList,
            Warehouse warehouse,String[] warehouseCodesArray) throws Exception {
         String htmlStart = "";
         String htmlEnd = "";
         htmlStart +="  <ul>    "
                   +"       <li id=\""+warehouse.getCode()+"\" >"
                   +"           <a href=\"#\">"+warehouse.getName()+"</a>";
         htmlEnd   +="      </li>"
                   +"   </ul>   ";
         return  htmlStart+htmlEnd;
    }
    
      /** 获得当前仓库及其下属仓库 字符串*/
    //获取当前仓库及下级仓库
      public static ObjectList fetchAllWarehouseCode(DBConnection con,String currWareCode) throws Exception
      {

            Values vs = new Values();
            DBEngine.checkCon(con);
            String sql = "select * from " + Warehouse.TABLE_NAME + " where " + Warehouse.CODE + " like ? and status='1' and " + Warehouse.CODE + " not like '000005%' and "+  Warehouse.CODE +" not in ('00000202','00000300','00000301','00000302') order by " + Warehouse.CODE;
            vs.add(currWareCode+"%");
            
            DBResult result = DBEngine.doSelect(con,sql,vs);
            
            ObjectList list = ObjectList.fromRs(Warehouse.class,result.getRs());
            result.clear();
            ObjectList returnList = ObjectList.getList();
            for (int i = 0; i < list.size(); i++) {
                Warehouse warehouse = (Warehouse) list.get(i);
                if(warehouse.getCode().length()>=8&& !warehouse.getCode().equals("00000401")&& !warehouse.getCode().equals("00000400")){
                    returnList.add(warehouse);
                }
            }
            return returnList;  
      } 
      
      /** 获取营运公司代码*/
          public static ObjectList fetchAllWarehouseCodeOfCompany(DBConnection con) throws Exception
          {

                Values vs = new Values();
                DBEngine.checkCon(con);
                String sql = "select * from " + Warehouse.TABLE_NAME + " where status=1 and is_show=1 order by " + Warehouse.CODE;
            
                DBResult result = DBEngine.doSelect(con,sql);
                
                ObjectList list = ObjectList.fromRs(Warehouse.class,result.getRs());
                result.clear();
                ObjectList returnList = ObjectList.getList();
                for (int i = 0; i < list.size(); i++) {
                    Warehouse warehouse = (Warehouse) list.get(i);
                    if(warehouse.getCode().length()==6){
                        returnList.add(warehouse);
                    }
                }
                return returnList;  
          } 

}

